package cn.gson.work.model.service;

import cn.gson.work.model.mapper.DeptMapper;
import cn.gson.work.model.pojos.Dept;
import cn.gson.work.model.pojos.User;
import cn.gson.work.model.vo.DeptQuery;
import cn.gson.work.model.vo.DeptTree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptService {
    @Autowired
    DeptMapper deptm;


    /**
     * 分页条件查询部门
     * @param query
     * @return
     */
    public Map<String,Object> getDept(DeptQuery query){
        List<Dept> list = deptm.getDept(query);
        System.out.println("*****"+list);
        List<Dept> depts =  new ArrayList<Dept>();
        if(list != null) {
            for(int i = 0; i < query.getPagesize(); i++){
                int n = (query.getCurpage() - 1) * query.getPagesize() + i;
                if(n<list.size()) {
                    Dept dept = list.get(n);
                    if(dept!=null) {
                        depts.add(dept);
                    }
                }
            }
        }
        Map map = new HashMap();
        map.put("total", list.size());
        map.put("list", depts);
        return map;
    }


    /**
     * 条件查询部门
     * @param query
     * @return
     */
    public Map<String,Object> getDeptList(DeptQuery query){
        Map<String, Object> map = new HashMap<>();
        List<Dept> list = deptm.getDeptList(query);
        if(list != null) {
            PageHelper.startPage(query.getCurpage(),query.getPagesize());
            PageInfo<Dept> info = new PageInfo<>(list);
            map.put("list",list);
            map.put("total",info.getTotal());
        }
        return map;
    }

    /**
     * 新增部门
     * @param dept
     */
    public void addDept(Dept dept){
        deptm.addDept(dept);
    }

    /**
     * 修改部门信息
     * @param dept
     */
    public void editDept(Dept dept){
        deptm.editDept(dept);
    }


    /**
     * 递归得到部门树形数据
     * @param query
     * @return
     */
    public List<DeptTree> getSortDeptList(DeptQuery query){
        System.out.println("49----");
        List<DeptTree> depttrees = new ArrayList<DeptTree>();
        List<Dept> list = deptm.getDeptList(query);
        System.out.println("95----"+list.toString());
        for (Dept de : list) {
            if(de.getInNo() == null) {
                depttrees.add(this.sortDeptList(de, list));
            }
        }
        System.out.println(depttrees.toString());
        return depttrees;
    }

    /**
     *  部门递归方法
     * @param dept
     * @param list
     * @return
     */
    public DeptTree sortDeptList(Dept dept,List<Dept> list) {
        DeptTree depttree = new DeptTree();
        depttree.setDeptNo(dept.getDeptNo());
        depttree.setDeptName(dept.getDeptName());
        depttree.setDept(dept);
        List<DeptTree> depttrees = new ArrayList<DeptTree>();
        //循环查出该网点的子网点
        for (Dept d : list) {
            if(d.getInNo()!=null && dept.getDeptNo() == d.getInNo().intValue()){
                //递归得到子网点
                DeptTree webtree2 = this.sortDeptList(d, list);
                depttrees.add(webtree2);
            }
        }
        depttree.setChildren(depttrees);
        return depttree;
    }

}
